mui.init();


document.addEventListener("dataList", function () {
	myColleData.getCollectionData('normal');
});

var myColleData;
mui.plusReady(function() {
	hideScrollBar();
	myColleData = new Vue({
		el: '#collection_lists',
		data: {
			lists: [],

			page:1,
			npage:1,
			cpage:1,
			epage:1,
			spage:1,

			dataUlList:0,

			articleList:[],
			courseList:[],
			ebookList:[],
			shopList:[],

			tabIndex: 1,
			auditStatus:'no',
			product_type:'article',

			miniRefreshArr:[],
		},
		watch:{
			tabIndex:function (val,oldVal) {
				var self = this;
				if(val == 1){
					self.product_type = 'article';
				}else if(val == 2){
					self.product_type = 'course';
				}else if(val == 3){
					self.product_type = 'ebook';
				}else if(val == 4){
					self.product_type = 'shop';
				}
				if(self.miniRefreshArr[val-1] == undefined){
					self.$nextTick(function () {
						self.miniRefreshArr[val-1] = new MiniRefresh({
							container: '#minirefresh' + val,
							down: {
								isAuto: true,
								callback: function() {
									self.getCollectionData('normal');
								}
							},
							up: {
								isAuto: false,
								offset:10,
								loadFull:{
									isEnable:false,
								},
								callback: function() {
									self.getCollectionData('up');
								}
							}
						});
					})

				}else{
					self.getCollectionData('tab');
				}
			}
		},
		mounted() {
			var self = this;
			self.$nextTick(function() {
				self.auditStatus = plus.storage.getItem('auditStatus') ? plus.storage.getItem('auditStatus') : 'no';
                var slider = mui("#slider");
                slider.slider({
                    interval: 0
                });
				self.miniRefreshArr[0] = new MiniRefresh({
					container: '#minirefresh1',
					down: {
						isAuto: true,
						callback: function() {
							self.getCollectionData('normal');
						}
					},
					up: {
						isAuto: false,
						offset:10,
						loadFull:{
							isEnable:false,
						},
						callback: function() {
							self.getCollectionData('up');
						}
					}
				});
			})
		},
		methods: {
			select_tap: function(e) {
				this.tabIndex = parseInt(e.target.getAttribute('tab-index'));
			},
			getCollectionData: function(type) {
				var self = this;
				if(type == 'normal'){
                    self.miniRefreshArr[self.tabIndex - 1].resetUpLoading();
					self.page = 1;
                    self.npage = 1;
                    self.cpage = 1;
                    self.epage = 1;
                    self.spage = 1;
					self.articleList.length = 0;
					self.courseList.length = 0;
					self.ebookList.length = 0;
					self.shopList.length = 0;
				}else if(type == 'up'){
					console.log('上拉');
					switch (self.tabIndex) {
						case 1:
							self.npage++;
							self.page = self.npage;
							break;
						case 2:
							self.cpage++;
							self.page = self.cpage;
							break;
						case 3:
							self.epage++;
							self.page = self.epage;
							break;
						case 4:
							self.spage++;
							self.page = self.spage;
							break;
					}
				}else if(type == 'tab'){
					self.miniRefreshArr[self.tabIndex - 1].resetUpLoading();
					switch (self.tabIndex) {
						case 1:
							if (self.articleList.length == 0) {
								self.page = self.npage = 1;
							}else{
								self.dataUlList = 0;
                                return false;
							}
							break;
						case 2:
							if (self.courseList.length == 0) {
								self.page = self.cpage = 1;
							}else{
								self.dataUlList = 0;
                                return false;
							}
							break;
						case 3:
							if (self.ebookList.length == 0) {
								self.page = self.epage = 1;
							}else{
								self.dataUlList = 0;
                                return false;
							}
							break;
						case 4:
							if (self.shopList.length == 0) {
								self.page = self.spage = 1;
							}else{
								self.dataUlList = 0;
                                return false;
							}
							break;
					}
				}
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/user/user/getMyCollection112',
					dataType: 'json',
					data: {
						'currentpage': self.page,
						'product_type': self.product_type
					},
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					timeout: 3000,
					type: 'get',
					success: function(msg) {
						if (msg.code == 1) {
							self.dataUlList = 0;
							switch (self.tabIndex) {
								case 1:
									$.each(msg.data, function (index, value) {
										self.articleList.push(value);
									});
									break;
								case 2:
									$.each(msg.data, function (index, value) {
										self.courseList.push(value);
									});
									break;
								case 3:
									$.each(msg.data, function (index, value) {
										self.ebookList.push(value);
									});
									break;
								case 4:
									$.each(msg.data, function (index, value) {
										self.shopList.push(value);
									});
									break;
							}
                            if(type == 'up'){
                                self.miniRefreshArr[self.tabIndex - 1].endUpLoading(false, '成功更新'+msg.data.length+'条数据');
                            }
						}else{
						    if(self.page == 1 && msg.data.length == 0){
                                self.dataUlList = 1;
                            }
                            if(type == 'up'){
                                self.miniRefreshArr[self.tabIndex - 1].endUpLoading(true);
                            }
						}
					},
					error: function() {
						self.dataUlList = 1;
					},
					complete: function() {
                        if(type == 'normal'){
                            setTimeout(function () {
                                self.miniRefreshArr[self.tabIndex - 1].endDownLoading(true);
                            },500);
                        }
					}
				})
			},
			open_class_detils: function(classid) {
				mui.openWindow({
					url: '../courseDetail/courseDetail.html',
					extras: {
						'classid': classid
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},

			open_ebook_detai: function(dataID) {
				//打开电子书详情页面
				mui.openWindow({
					url: '../eBook/eBookDetail.html',
					id: 'ebookDetail.html',
					extras: {
						'bookid': dataID
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},

			favorite_news: function(id) {
				mui.openWindow({
					url: '../news/detail.html',
					extras: {
						'newsid': id
					},
					show: {
						autoShow: true,
						aniShow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
						title: '加载中...',
						options: {},
					}
				});
			},
			favorite_course: function(id) {
				mui.openWindow({
					url: '../courseDetail/courseDetail.html',
					extras: {
						'cid': id
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},
			favorite_eBook: function(id) {
				mui.openWindow({
					url: '../eBook/eBookDetail.html',
					extras: {
						'newsid': id
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},
			favorite_mallList: function(id) {
				mui.openWindow({
					url: '../courseDetail/mallDetail.html',
					extras: {
						'product_id': id
					},
					createNew: false,
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				});
			},
			TimeToStamp: function(time) {
				var date = new Date(time);
				var Y = date.getFullYear();
				var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
				var D = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate();
				return Y + '-' + M + D;
			}
		}
	})
})

